using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameStateManagement;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;

namespace GameStateManagementSample.Screens
{
    class MenuScreen : GameScreen
    {
#region Fields
        ContentManager m_Content;
        Texture2D m_SBack;
        InputAction pauseAction;
        Rectangle m_BTPlay,m_BTAbout,m_BTHelp;
#endregion
#region Initialization
        public MenuScreen()
        {
            TransitionOnTime = TimeSpan.FromSeconds(0.75f);
            TransitionOffTime = TimeSpan.FromSeconds(0.5f);
            pauseAction = new InputAction(
                new Buttons[] { Buttons.Start, Buttons.Back },
                new Keys[] { Keys.Escape },
                true);
        }
        public override void Activate(bool instancePreserved)
        {
            if (!instancePreserved)
            {
                if (m_Content == null)
                {
                    m_Content = new ContentManager(ScreenManager.Game.Services, "Content");
                    ScreenManager.Game.ResetElapsedTime();
                    m_SBack = m_Content.Load<Texture2D>("Image/menu_text");

                    m_BTPlay = new Rectangle(100,300,280,70);
                    m_BTAbout = new Rectangle(260,575, 220, 67);
                    m_BTHelp = new Rectangle(260, 675, 220, 67);
                }
            }
        }
        public override void Deactivate()
        {
            base.Deactivate();
        }
        public override void Unload()
        {
            base.Unload();
        }
#endregion
#region Update and Draw
        protected virtual void OnCancel() 
        {
            ExitScreen();
        }
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }
        public override void HandleInput(Microsoft.Xna.Framework.GameTime gameTime, InputState input)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            PlayerIndex player;
            if (pauseAction.Evaluate(input,ControllingPlayer,out player))
            {
                ScreenManager.AddScreen(new Exit(), PlayerIndex.One);
            }
            if (input.TouchState.Count > 0)
            {
                //ScreenManager.AddScreen(new PlayScreen(), null);
            }
            // Read in our gestures
            TouchCollection touches = input.TouchState;

            foreach (TouchLocation touch in touches)
            {
                if (touch.State == TouchLocationState.Pressed)
                {
                    if (m_BTPlay.Contains((int)touch.Position.X, (int)touch.Position.Y))
                    {
                        ExitScreen();
                        ScreenManager.AddScreen(new PlayScreen(), null);
                    }
                    if (m_BTAbout.Contains((int)touch.Position.X, (int)touch.Position.Y))
                    {
                        ScreenManager.AddScreen(new AboutScreen(), null);
                    }
                    if (m_BTHelp.Contains((int)touch.Position.X, (int)touch.Position.Y))
                    {
                        ScreenManager.AddScreen(new HelpScreen(), null);
                    }
                }
            }
          
        }
        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

            spriteBatch.Begin();
            spriteBatch.Draw(m_SBack, new Vector2(0, (TransitionPosition * 200)), Color.White * TransitionAlpha);            
            spriteBatch.End();
        }
#endregion
    }
}
